package com.ingesup.ws.models;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ingesup.core.GetConnection;
import com.ingesup.core.Util;

public class User {
	private long id;
	private String firstName;
	private String lastName;
	private String email;
	private String password;
	private String phone;
	private String address;

	private UserType type;

	public User(){}
	
	public User(long id) throws Exception {
		try {
			PreparedStatement stm = GetConnection.getConnection().prepareStatement("select id from users where id='?'");
			stm.setLong(1, id);
			ResultSet res = stm.executeQuery();
			if(res.next()){
				this.id = res.getLong("id");
				this.email = res.getString("email");
				this.password = res.getString("password");
				this.firstName = res.getString("first_name");
				this.lastName = res.getString("last_name");
				this.phone = res.getString("phone");
				this.address = res.getString("address");
				this.type = UserType.valueOf(res.getString("type"));
			}else{	
				throw new Exception("Impossible de trouver l'utilisateur avec l'id "+id);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new Exception("Impossible de trouver l'utilisateur avec l'id "+id);
		}
	}

	public String getAddress() {
		return address;
	}

	public String getEmail() {
		return email;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getPassword() {
		return password;
	}

	public String getPhone() {
		return phone;
	}

	public UserType getType() {
		return type;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public void setEmail(String email) {
		this.email = email;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setType(UserType type) {
		this.type = type;
	}

	
	
	
	public static boolean create(String email,String password, String firstName,String lastName, String phone, String address, UserType type ){
		try {
			PreparedStatement stm = GetConnection.getConnection().prepareStatement("insert into users(email,password,first_name,last_name,phone,address,type) values('?','?','?','?','?','?','?')");
			stm.setString(1, email);
			stm.setString(2, Util.md5(password));
			stm.setString(3, firstName);
			stm.setString(4, lastName);
			stm.setString(5, phone);
			stm.setString(6, address);
			stm.setString(7, type.toString());
			stm.execute();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean create(User u){
		return create(u.getEmail(),u.getPassword(),u.getFirstName(),u.getLastName(),u.getPhone(),u.getAddress(),u.getType());	
	}

	public static boolean edit(long id,String email,String password, String firstName,String lastName, String phone, String address, UserType type ){
		try {
			PreparedStatement stm = GetConnection.getConnection().prepareStatement("update users set " +
																			"email='?', " +
																			"password='?', " +
																			"first_name='?', " +
																			"last_name='?', " +
																			"phone='?', " +
																			"address='?'," +
																			"type='?' " +
																			"where id = ?");
			stm.setString(1, email);
			stm.setString(2, Util.md5(password));
			stm.setString(3, firstName);
			stm.setString(4, lastName);
			stm.setString(5, phone);
			stm.setString(6, address);
			stm.setString(7, type.toString());
			stm.setLong(8, id);
			stm.execute();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	

	public static int connect(String email,String password){
		try {
			PreparedStatement stm = GetConnection.getConnection().prepareStatement("select id from users where email='?' and password='?'");
			stm.setString(1, email);
			stm.setString(2, Util.md5(password));
			ResultSet res = stm.executeQuery();
			if(res.next()){
				return res.getInt("id");
			}else{	
				return -1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	public static User find(long id) throws Exception{
		return new User(id);
	}
	
}
